package 树.遍历;


import 抽象数据类型.TreeNode;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class LevelTraverse {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        levelOrder(root);
    }

    //水平遍历
    public static void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque();
        queue.offer(root);
        int level = 0;
        Stack stack = new Stack();
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- != 0) {
                root = queue.poll();
                System.out.print(root.val);
                if (root.left != null) {
                    queue.offer(root.left);
                }
                if (root.right != null) {
                    queue.offer(root.right);
                }
                stack.push(root.getVal());
            }
            level++;
        }
        System.out.println();
        System.out.print("depth: " + level);
    }
}
